import ValidAuth from './ValidAuth'
import { routesMap } from '../router/routeMap'
import { RouteObject } from 'react-router-dom'

type RouteKey = keyof typeof routesMap
interface MenuListType {
  label: string
  key: RouteKey
  icon?: string
  children?: MenuListType[]
}
type MyRoute = RouteObject & {
  title?: string
}
function gernateRouters(menuList: MenuListType[]): MyRoute[] {
  // 遍历菜单, 对 路由的 path  element 和 children 进行赋值
  return menuList.map((item: MenuListType) => {
    let routeC: MyRoute = {
      path: item.key,
      title: item.label,
      element: item.children ? null : (
        <>
          <ValidAuth needLogin={true} redirectTo="/login">
            <>{routesMap[item.key]}</>
          </ValidAuth>
        </>
      )
    }
    if (item.children) {
      routeC.children = gernateRouters(item.children)
    }
    return routeC
  })
}

export default gernateRouters
